

<!DOCTYPE html>
<html lang="zh-CN" data-default-color-scheme=auto>



<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="/img/favicon.png">
  <link rel="icon" href="/img/favicon.png">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=5.0, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="author" content="Han Qiu">
  <meta name="keywords" content="">
  
    <meta name="description" content="1. number 所有数字都是浮点数，类型为 number，支持二进制、八进制、十进制和十六进制  12345678&#x2F;&#x2F; 十进制let decimal: number &#x3D; 6;&#x2F;&#x2F; 十六进制let hex: number &#x3D; 0xf00d;&#x2F;&#x2F; 二进制let binary: number &#x3D; 0b1010;&#x2F;&#x2F; 八进制let octal: number &#x3D; 0o744;  2. string12">
<meta property="og:type" content="article">
<meta property="og:title" content="ts数据类型">
<meta property="og:url" content="http://example.com/2022/02/24/ts%E6%95%B0%E6%8D%AE%E7%B1%BB%E5%9E%8B/index.html">
<meta property="og:site_name" content="寒秋の博客">
<meta property="og:description" content="1. number 所有数字都是浮点数，类型为 number，支持二进制、八进制、十进制和十六进制  12345678&#x2F;&#x2F; 十进制let decimal: number &#x3D; 6;&#x2F;&#x2F; 十六进制let hex: number &#x3D; 0xf00d;&#x2F;&#x2F; 二进制let binary: number &#x3D; 0b1010;&#x2F;&#x2F; 八进制let octal: number &#x3D; 0o744;  2. string12">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2022-02-24T08:10:15.000Z">
<meta property="article:modified_time" content="2022-02-24T08:15:08.743Z">
<meta property="article:author" content="Han Qiu">
<meta name="twitter:card" content="summary_large_image">
  
  
  <title>ts数据类型 - 寒秋の博客</title>

  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4/dist/css/bootstrap.min.css" />


  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/github-markdown-css@4/github-markdown.min.css" />
  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/hint.css@2/hint.min.css" />

  
    
    
      
      <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/highlight.js@10/styles/github-gist.min.css" />
    
  

  
    <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3/dist/jquery.fancybox.min.css" />
  


<!-- 主题依赖的图标库，不要自行修改 -->

<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_ba1fz6golrf.css">



<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_lbnruvf0jn.css">


<link  rel="stylesheet" href="/css/main.css" />

<!-- 自定义样式保持在最底部 -->


  <script id="fluid-configs">
    var Fluid = window.Fluid || {};
    var CONFIG = {"hostname":"example.com","root":"/","version":"1.8.14","typing":{"enable":true,"typeSpeed":70,"cursorChar":"_","loop":false},"anchorjs":{"enable":true,"element":"h1,h2,h3,h4,h5,h6","placement":"right","visible":"hover","icon":""},"progressbar":{"enable":true,"height_px":3,"color":"#29d","options":{"showSpinner":false,"trickleSpeed":100}},"copy_btn":true,"image_zoom":{"enable":true,"img_url_replace":["",""]},"toc":{"enable":true,"headingSelector":"h1,h2,h3,h4,h5,h6","collapseDepth":0},"lazyload":{"enable":true,"loading_img":"/img/loading.gif","onlypost":false,"offset_factor":2},"web_analytics":{"enable":false,"baidu":null,"google":null,"gtag":null,"tencent":{"sid":null,"cid":null},"woyaola":null,"cnzz":null,"leancloud":{"app_id":null,"app_key":null,"server_url":null,"path":"window.location.pathname","ignore_local":false}},"search_path":"/local-search.xml"};
  </script>
  <script  src="/js/utils.js" ></script>
  <script  src="/js/color-schema.js" ></script>
<meta name="generator" content="Hexo 6.0.0"></head>


<body>
  <header style="height: 70vh;">
    <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand" href="/">
      <strong>blogs</strong>
    </a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/">
                <i class="iconfont icon-home-fill"></i>
                首页
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/archives/">
                <i class="iconfont icon-archive-fill"></i>
                归档
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/categories/">
                <i class="iconfont icon-category-fill"></i>
                分类
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/tags/">
                <i class="iconfont icon-tags-fill"></i>
                标签
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/about/">
                <i class="iconfont icon-user-fill"></i>
                关于
              </a>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" target="_self" href="javascript:;" data-toggle="modal" data-target="#modalSearch" aria-label="Search">
              &nbsp;<i class="iconfont icon-search"></i>&nbsp;
            </a>
          </li>
        
        
          <li class="nav-item" id="color-toggle-btn">
            <a class="nav-link" target="_self" href="javascript:;" aria-label="Color Toggle">&nbsp;<i
                class="iconfont icon-dark" id="color-toggle-icon"></i>&nbsp;</a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

    <div class="banner" id="banner" parallax=true
         style="background: url('/img/default.png') no-repeat center center;
           background-size: cover;">
      <div class="full-bg-img">
        <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
          <div class="page-header text-center fade-in-up">
            <span class="h2" id="subtitle" title="ts数据类型">
              
            </span>

            
              <div class="mt-3">
  
  
    <span class="post-meta">
      <i class="iconfont icon-date-fill" aria-hidden="true"></i>
      <time datetime="2022-02-24 16:10" pubdate>
        2022年2月24日 下午
      </time>
    </span>
  
</div>

<div class="mt-1">
  
    <span class="post-meta mr-2">
      <i class="iconfont icon-chart"></i>
      3.6k 字
    </span>
  

  
    <span class="post-meta mr-2">
      <i class="iconfont icon-clock-fill"></i>
      
      
      31 分钟
    </span>
  

  
  
</div>

            
          </div>

          
        </div>
      </div>
    </div>
  </header>

  <main>
    
      

<div class="container-fluid nopadding-x">
  <div class="row nomargin-x">
    <div class="d-none d-lg-block col-lg-2"></div>
    <div class="col-lg-8 nopadding-x-md">
      <div class="container nopadding-x-md" id="board-ctn">
        <div class="py-5" id="board">
          <article class="post-content mx-auto">
            <!-- SEO header -->
            <h1 style="display: none">ts数据类型</h1>
            
            <div class="markdown-body">
              <h4 id="1-number"><a href="#1-number" class="headerlink" title="1. number"></a>1. number</h4><ul>
<li>所有数字都是浮点数，类型为 number，支持二进制、八进制、十进制和十六进制</li>
</ul>
<figure class="highlight ts"><table><tr><td class="gutter"><div class="code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></div></td><td class="code"><pre><code class="hljs ts"><span class="hljs-comment">// 十进制</span><br><span class="hljs-keyword">let</span> decimal: <span class="hljs-built_in">number</span> = <span class="hljs-number">6</span>;<br><span class="hljs-comment">// 十六进制</span><br><span class="hljs-keyword">let</span> hex: <span class="hljs-built_in">number</span> = <span class="hljs-number">0xf00d</span>;<br><span class="hljs-comment">// 二进制</span><br><span class="hljs-keyword">let</span> binary: <span class="hljs-built_in">number</span> = <span class="hljs-number">0b1010</span>;<br><span class="hljs-comment">// 八进制</span><br><span class="hljs-keyword">let</span> octal: <span class="hljs-built_in">number</span> = <span class="hljs-number">0o744</span>;<br></code></pre></td></tr></table></figure>

<h4 id="2-string"><a href="#2-string" class="headerlink" title="2. string"></a>2. string</h4><figure class="highlight ts"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><code class="hljs ts"><span class="hljs-keyword">let</span> color: <span class="hljs-built_in">string</span> = <span class="hljs-string">&quot;blue&quot;</span>;<br>color = <span class="hljs-string">&#x27;red&#x27;</span>;<br></code></pre></td></tr></table></figure>

<h4 id="3-boolean"><a href="#3-boolean" class="headerlink" title="3. boolean"></a>3. boolean</h4><figure class="highlight ts"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs ts"><span class="hljs-keyword">let</span> isDone: <span class="hljs-built_in">boolean</span> = <span class="hljs-literal">false</span>;<br></code></pre></td></tr></table></figure>

<h4 id="4-enum"><a href="#4-enum" class="headerlink" title="4. enum"></a>4. enum</h4><figure class="highlight ts"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><code class="hljs ts"><span class="hljs-built_in">enum</span> Color &#123;<br>    Red,<br>    Green,<br>    Blue<br>&#125;<br><span class="hljs-keyword">let</span> c: Color = Color.Green;<br></code></pre></td></tr></table></figure>

<h4 id="5-array"><a href="#5-array" class="headerlink" title="5. array"></a>5. array</h4><ul>
<li>数组定义方式一：<code>元素类型[]</code></li>
<li>数组定义方式二：<code>Array&lt;元素类型&gt;</code></li>
</ul>
<figure class="highlight ts"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><code class="hljs ts"><span class="hljs-keyword">let</span> list: <span class="hljs-built_in">number</span>[] = [<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>];<br><span class="hljs-keyword">let</span> list: <span class="hljs-built_in">Array</span>&lt;<span class="hljs-built_in">number</span>&gt; = [<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>];<br></code></pre></td></tr></table></figure>

<h4 id="6-tuple"><a href="#6-tuple" class="headerlink" title="6. tuple"></a>6. tuple</h4><ul>
<li>元组，表示已知元素数量和类型的数组</li>
</ul>
<figure class="highlight ts"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs ts"><span class="hljs-keyword">let</span> x: [<span class="hljs-built_in">string</span>, <span class="hljs-built_in">number</span>] = [<span class="hljs-string">&quot;hello&quot;</span>, <span class="hljs-number">10</span>];<br></code></pre></td></tr></table></figure>

<h4 id="7-any"><a href="#7-any" class="headerlink" title="7. any"></a>7. any</h4><ul>
<li>表示任意类型。会关闭类型检测，不建议使用</li>
</ul>
<figure class="highlight ts"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs ts"><span class="hljs-keyword">let</span> d: <span class="hljs-built_in">any</span> = <span class="hljs-number">4</span>;<br>d = <span class="hljs-string">&#x27;hello&#x27;</span>;<br>d = <span class="hljs-literal">true</span>;<br></code></pre></td></tr></table></figure>

<h4 id="8-null-和-undefined"><a href="#8-null-和-undefined" class="headerlink" title="8. null 和 undefined"></a>8. null 和 undefined</h4><figure class="highlight ts"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><code class="hljs ts"><span class="hljs-keyword">let</span> n: <span class="hljs-literal">null</span> = <span class="hljs-literal">null</span>;<br><span class="hljs-keyword">let</span> u: <span class="hljs-literal">undefined</span> = <span class="hljs-literal">undefined</span>;<br></code></pre></td></tr></table></figure>

<h4 id="9-unknown"><a href="#9-unknown" class="headerlink" title="9. unknown"></a>9. unknown</h4><ul>
<li>unknown 和 any 的主要区别在于 unknown 在操作前会对类型进行检查，而 any 不会进行类型检查</li>
<li>所有类型都可以赋值给 unknown，unknown 类型只能赋值给 any 和 unknown 类型</li>
</ul>
<figure class="highlight ts"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><code class="hljs ts"><span class="hljs-keyword">let</span> notSure: unknown = <span class="hljs-number">4</span>;<br>notSure = <span class="hljs-string">&#x27;hello&#x27;</span>;<br></code></pre></td></tr></table></figure>

<h4 id="10-void"><a href="#10-void" class="headerlink" title="10. void"></a>10. void</h4><ul>
<li>表示没有任何类型。通常一个函数没有返回值时，其返回值类型为 void</li>
</ul>
<figure class="highlight ts"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs ts"><span class="hljs-keyword">let</span> unusable: <span class="hljs-built_in">void</span> = <span class="hljs-literal">undefined</span>;<br></code></pre></td></tr></table></figure>

<h4 id="11-never"><a href="#11-never" class="headerlink" title="11. never"></a>11. never</h4><ul>
<li>表示永远不存在的值的类型。never类型是那些<code>总是会抛出异常</code>或<code>根本就不会有返回值</code>的函数表达式或箭头函数表达式的返回值类型</li>
<li>never 和 null 的区别在于 void 可以被赋值为 null 和undefined 类型，never 则是一个不包含值的类型。拥有 void 返回值类型的函数能正常运行，拥有 never 返回值类型的函数无法正常返回，无法终止，或会抛出异常</li>
</ul>
<figure class="highlight ts"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs ts"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">error</span>(<span class="hljs-params">message: <span class="hljs-built_in">string</span></span>): <span class="hljs-title">never</span> </span>&#123;<br>    <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-built_in">Error</span>(message);<br>&#125;<br></code></pre></td></tr></table></figure>

<h4 id="12-symbol"><a href="#12-symbol" class="headerlink" title="12. symbol"></a>12. symbol</h4><ul>
<li>ES6 中新增数据类型，用来表示独一无二的值</li>
<li>在使用 symbol 时，必须在 tsconfig.json 的 libs 字段中 ES 版本至少为 ES2015 或 ES6</li>
</ul>
<figure class="highlight ts"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs ts"><span class="hljs-keyword">const</span> sym1:symbol = <span class="hljs-built_in">Symbol</span>(<span class="hljs-string">&#x27;hello&#x27;</span>);<br><span class="hljs-keyword">const</span> sym2:symbol = <span class="hljs-built_in">Symbol</span>(<span class="hljs-string">&#x27;hello&#x27;</span>);<br><span class="hljs-built_in">console</span>.log(sym1 === sym2); <span class="hljs-comment">// false</span><br></code></pre></td></tr></table></figure>

<h4 id="13-bigint"><a href="#13-bigint" class="headerlink" title="13. bigint"></a>13. bigint</h4><ul>
<li>ES11 中新增数据类型，可以安全地存储和操作大整数</li>
<li>在使用 bigint 时，必须在 tsconfig.json 的 libs 字段中 ES 版本至少为 ES2020</li>
</ul>
<figure class="highlight ts"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><code class="hljs ts"><span class="hljs-keyword">const</span> max1 = <span class="hljs-built_in">Number</span>.MAX_SAFE_INTEGER;   <span class="hljs-comment">// 2**53-1</span><br><span class="hljs-built_in">console</span>.log(max1 + <span class="hljs-number">1</span> === max1 + <span class="hljs-number">2</span>); <span class="hljs-comment">// true</span><br><span class="hljs-keyword">const</span> max2 = <span class="hljs-built_in">BigInt</span>(<span class="hljs-built_in">Number</span>.MAX_SAFE_INTEGER); <br><span class="hljs-built_in">console</span>.log(max2 + <span class="hljs-number">1n</span> === max2 + <span class="hljs-number">2n</span>);   <span class="hljs-comment">// false</span><br></code></pre></td></tr></table></figure>

<h4 id="14-object、Object-和-类型"><a href="#14-object、Object-和-类型" class="headerlink" title="14. object、Object 和 {} 类型"></a>14. object、Object 和 {} 类型</h4><ul>
<li>object 用于表示非原始类型，也就是除number、string、boolean、symbol、bigint、null 或 undefined 之外的类型</li>
<li>Object 和 {} 一样，代表所有拥有 toString、hasOwnProperty 方法的类型。所有原始类型、非原始类型都可以赋给 Object（严格模式下 null 和 undefined 不可以）</li>
</ul>
<figure class="highlight ts"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><code class="hljs ts"><span class="hljs-comment">// object 类型</span><br><span class="hljs-keyword">let</span> objectCase: <span class="hljs-built_in">object</span>;<br>objectCase = <span class="hljs-number">1</span>; <span class="hljs-comment">// error</span><br>objectCase = <span class="hljs-string">&quot;a&quot;</span>; <span class="hljs-comment">// error</span><br>objectCase = <span class="hljs-literal">true</span>; <span class="hljs-comment">// error</span><br>objectCase = <span class="hljs-literal">null</span>; <span class="hljs-comment">// error</span><br>objectCase = <span class="hljs-literal">undefined</span>; <span class="hljs-comment">// error</span><br>objectCase = &#123;&#125;; <span class="hljs-comment">// ok</span><br><span class="hljs-comment">// Object 类型</span><br><span class="hljs-keyword">let</span> ObjectCase: <span class="hljs-built_in">Object</span>;<br>ObjectCase = <span class="hljs-number">1</span>; <span class="hljs-comment">// ok</span><br>ObjectCase = <span class="hljs-string">&quot;a&quot;</span>; <span class="hljs-comment">// ok</span><br>ObjectCase = <span class="hljs-literal">true</span>; <span class="hljs-comment">// ok</span><br>ObjectCase = <span class="hljs-literal">null</span>; <span class="hljs-comment">// error</span><br>ObjectCase = <span class="hljs-literal">undefined</span>; <span class="hljs-comment">// error</span><br>ObjectCase = &#123;&#125;; <span class="hljs-comment">// ok</span><br><span class="hljs-comment">// &#123;&#125; 类型</span><br><span class="hljs-keyword">let</span> simpleCase: &#123;&#125;;<br>simpleCase = <span class="hljs-number">1</span>; <span class="hljs-comment">// ok</span><br>simpleCase = <span class="hljs-string">&quot;a&quot;</span>; <span class="hljs-comment">// ok</span><br>simpleCase = <span class="hljs-literal">true</span>; <span class="hljs-comment">// ok</span><br>simpleCase = <span class="hljs-literal">null</span>; <span class="hljs-comment">// error</span><br>simpleCase = <span class="hljs-literal">undefined</span>; <span class="hljs-comment">// error</span><br>simpleCase = &#123;&#125;; <span class="hljs-comment">// ok</span><br></code></pre></td></tr></table></figure>

<h4 id="15-字面量"><a href="#15-字面量" class="headerlink" title="15. 字面量"></a>15. 字面量</h4><ul>
<li>可以使用字面量去指定变量的类型，通过字面量可以确定变量的取值范围</li>
</ul>
<figure class="highlight ts"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><code class="hljs ts"><span class="hljs-keyword">let</span> color: <span class="hljs-string">&#x27;red&#x27;</span> | <span class="hljs-string">&#x27;blue&#x27;</span> | <span class="hljs-string">&#x27;black&#x27;</span> = <span class="hljs-string">&#x27;red&#x27;</span>;<br><span class="hljs-keyword">let</span> num: <span class="hljs-number">1</span> = <span class="hljs-number">1</span>;<br></code></pre></td></tr></table></figure>

<h4 id="15-类型断言"><a href="#15-类型断言" class="headerlink" title="15. 类型断言"></a>15. 类型断言</h4><ul>
<li>有些情况下，变量的类型对于我们来说是很明确，但是TS编译器却并不清楚，此时，可以通过类型断言来告诉编译器变量的类型，断言有两种形式：</li>
</ul>
<figure class="highlight ts"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><code class="hljs ts"><span class="hljs-comment">// 1. as 语法</span><br><span class="hljs-keyword">let</span> someValue: unknown = <span class="hljs-string">&quot;this is a string&quot;</span>;<br><span class="hljs-keyword">let</span> strLength: <span class="hljs-built_in">number</span> = (someValue <span class="hljs-keyword">as</span> <span class="hljs-built_in">string</span>).length;<br><span class="hljs-comment">// 2. 尖括号语法</span><br><span class="hljs-keyword">let</span> someValue: unknown = <span class="hljs-string">&quot;this is a string&quot;</span>;<br><span class="hljs-keyword">let</span> strLength: <span class="hljs-built_in">number</span> = (&lt;<span class="hljs-built_in">string</span>&gt;someValue).length;<br></code></pre></td></tr></table></figure>

<blockquote>
<p>以上两种方式虽然没有任何区别，但是尖括号格式会与 react 中 JSX 产生语法冲突，因此更推荐使用 as 语法</p>
</blockquote>
<h4 id="16-非空断言"><a href="#16-非空断言" class="headerlink" title="16. 非空断言"></a>16. 非空断言</h4><ul>
<li>使用 <code>!</code> 可以用于断言操作对象是非 null 和非 undefined 类型 </li>
</ul>
<figure class="highlight ts"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs ts"><span class="hljs-keyword">let</span> flag: <span class="hljs-literal">null</span> | <span class="hljs-literal">undefined</span> | <span class="hljs-built_in">string</span>;<br>flag!.toString();   <span class="hljs-comment">// ok</span><br>flag.toString();    <span class="hljs-comment">// error</span><br></code></pre></td></tr></table></figure>

<h4 id="17-类型别名"><a href="#17-类型别名" class="headerlink" title="17. 类型别名"></a>17. 类型别名</h4><ul>
<li>给一个类型起一个新名字</li>
</ul>
<figure class="highlight ts"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs ts"><span class="hljs-keyword">type</span> flag = <span class="hljs-built_in">string</span>;<br><span class="hljs-comment">// type flag = string | number;</span><br><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">fn</span>(<span class="hljs-params">value: flag</span>) </span>&#123;&#125;<br></code></pre></td></tr></table></figure>

<h4 id="18-联合类型"><a href="#18-联合类型" class="headerlink" title="18. 联合类型"></a>18. 联合类型</h4><ul>
<li>联合类型表示取值可以为多种类型中的一种。未赋值时联合类型上只能访问两个类型共有的属性和方法</li>
</ul>
<figure class="highlight ts"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><code class="hljs ts"><span class="hljs-keyword">let</span> name: <span class="hljs-built_in">string</span> | <span class="hljs-built_in">number</span>;<br><span class="hljs-built_in">console</span>.log(name.toString());   <span class="hljs-comment">// 报错，name 赋值前不能使用</span><br>name = <span class="hljs-number">1</span>;<br><span class="hljs-built_in">console</span>.log(name.toFixed(<span class="hljs-number">2</span>));   <span class="hljs-comment">// 1.00</span><br>name = <span class="hljs-string">&#x27;hello&#x27;</span>;<br><span class="hljs-built_in">console</span>.log(name.length);   <span class="hljs-comment">// 5</span><br><br><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">fn</span>(<span class="hljs-params">name: <span class="hljs-built_in">string</span> | <span class="hljs-built_in">number</span></span>): <span class="hljs-title">void</span> </span>&#123;<br>  name.toString();<br>  name.toFixed(<span class="hljs-number">2</span>);  <span class="hljs-comment">// 报错，name 上不存在 toFixed 方法</span><br>  name.length;  <span class="hljs-comment">// 报错，name 上不存在 length 属性</span><br>&#125;<br></code></pre></td></tr></table></figure>

<h4 id="19-交叉类型"><a href="#19-交叉类型" class="headerlink" title="19. 交叉类型"></a>19. 交叉类型</h4><ul>
<li>交叉类型是将多个类型合并为一个类型。通过 &amp; 运算符可以将现有的多种类型叠加到一起成为一种类型，它包含了所需的所有类型的特性</li>
</ul>
<figure class="highlight ts"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><code class="hljs ts"><span class="hljs-keyword">type</span> Flag1 = &#123; <span class="hljs-attr">x</span>: <span class="hljs-built_in">number</span> &#125;;<br><span class="hljs-keyword">type</span> Flag2 = Flag1 &amp; &#123; <span class="hljs-attr">y</span>: <span class="hljs-built_in">string</span> &#125;;<br><br><span class="hljs-keyword">let</span> flag3: Flag2 = &#123;<br>  <span class="hljs-attr">x</span>: <span class="hljs-number">1</span>,<br>  <span class="hljs-attr">y</span>: <span class="hljs-string">&quot;hello&quot;</span><br>&#125;;<br></code></pre></td></tr></table></figure>

<blockquote>
<p>参考资料：<br><a target="_blank" rel="noopener" href="https://juejin.cn/post/7031787942691471396#heading-21">掘金：最全的TypeScript学习指南</a><br><a target="_blank" rel="noopener" href="https://www.tslang.cn/docs/handbook/basic-types.html">TypeScript 官方文档</a></p>
</blockquote>

            </div>
            <hr>
            <div>
              <div class="post-metas mb-3">
                
                  <div class="post-meta mr-3">
                    <i class="iconfont icon-category"></i>
                    
                      <a class="hover-with-bg" href="/categories/ts/">ts</a>
                    
                  </div>
                
                
              </div>
              
                <p class="note note-warning">
                  
                    本博客所有文章除特别声明外，均采用 <a target="_blank" href="https://creativecommons.org/licenses/by-sa/4.0/deed.zh" rel="nofollow noopener noopener">CC BY-SA 4.0 协议</a> ，转载请注明出处！
                  
                </p>
              
              
                <div class="post-prevnext">
                  <article class="post-prev col-6">
                    
                    
                      <a href="/2022/02/24/webpack%E6%89%93%E5%8C%85ts/">
                        <i class="iconfont icon-arrowleft"></i>
                        <span class="hidden-mobile">webpack打包ts</span>
                        <span class="visible-mobile">上一篇</span>
                      </a>
                    
                  </article>
                  <article class="post-next col-6">
                    
                    
                      <a href="/2022/02/14/bootstrap%E5%93%8D%E5%BA%94%E5%BC%8F%E5%B8%83%E5%B1%80/">
                        <span class="hidden-mobile">bootstrap响应式布局</span>
                        <span class="visible-mobile">下一篇</span>
                        <i class="iconfont icon-arrowright"></i>
                      </a>
                    
                  </article>
                </div>
              
            </div>

            
          </article>
        </div>
      </div>
    </div>
    
      <div class="d-none d-lg-block col-lg-2 toc-container" id="toc-ctn">
        <div id="toc">
  <p class="toc-header"><i class="iconfont icon-list"></i>&nbsp;目录</p>
  <div class="toc-body" id="toc-body"></div>
</div>

      </div>
    
  </div>
</div>

<!-- Custom -->


    

    
      <a id="scroll-top-button" aria-label="TOP" href="#" role="button">
        <i class="iconfont icon-arrowup" aria-hidden="true"></i>
      </a>
    

    
      <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v"
                 for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>
    

    
  </main>

  <footer class="text-center mt-5 py-3">
  <div class="footer-content">
     <a href="https://hexo.io" target="_blank" rel="nofollow noopener"><span>Hexo</span></a> <i class="iconfont icon-love"></i> <a href="https://github.com/fluid-dev/hexo-theme-fluid" target="_blank" rel="nofollow noopener"><span>Fluid</span></a> 
  </div>
  

  

  
</footer>


  <!-- SCRIPTS -->
  
  <script  src="https://cdn.jsdelivr.net/npm/nprogress@0/nprogress.min.js" ></script>
  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/nprogress@0/nprogress.min.css" />

  <script>
    NProgress.configure({"showSpinner":false,"trickleSpeed":100})
    NProgress.start()
    window.addEventListener('load', function() {
      NProgress.done();
    })
  </script>


<script  src="https://cdn.jsdelivr.net/npm/jquery@3/dist/jquery.min.js" ></script>
<script  src="https://cdn.jsdelivr.net/npm/bootstrap@4/dist/js/bootstrap.min.js" ></script>
<script  src="/js/events.js" ></script>
<script  src="/js/plugins.js" ></script>

<!-- Plugins -->


  <script  src="/js/local-search.js" ></script>



  
    <script  src="/js/img-lazyload.js" ></script>
  



  



  
    <script  src="https://cdn.jsdelivr.net/npm/tocbot@4/dist/tocbot.min.js" ></script>
  
  
    <script  src="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3/dist/jquery.fancybox.min.js" ></script>
  
  
    <script  src="https://cdn.jsdelivr.net/npm/anchor-js@4/anchor.min.js" ></script>
  
  
    <script defer src="https://cdn.jsdelivr.net/npm/clipboard@2/dist/clipboard.min.js" ></script>
  






  <script  src="https://cdn.jsdelivr.net/npm/typed.js@2/lib/typed.min.js" ></script>
  <script>
    (function (window, document) {
      var typing = Fluid.plugins.typing;
      var title = document.getElementById('subtitle').title;
      
        typing(title);
      
    })(window, document);
  </script>















<!-- 主题的启动项 保持在最底部 -->
<script  src="/js/boot.js" ></script>


</body>
</html>
